package com.ruoyi.product.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ruoyi.product.domain.ProductCategory;
import com.ruoyi.product.domain.Shopping.*;
import com.ruoyi.product.domain.SpecProduct;
import com.ruoyi.product.domain.vo.SpecProductVo;
import com.ruoyi.product.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.ManagementMapper;
import com.ruoyi.product.service.IManagementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品管理表Service业务层处理
 * 
 * @author cs
 * @date 2024-07-26
 */
@Service
public class ManagementServiceImpl implements IManagementService 
{
    @Autowired
    private ManagementMapper managementMapper;
    @Autowired
    private BasicMapper basicMapper;
    @Autowired
    private DetailsMapper detailsMapper;
    @Autowired
    private LogisticsMapper logisticsMapper;
    @Autowired
    private MarketingMapper marketingMapper;
    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private OtherMapper otherMapper;

    /**
     * 查询商品管理表
     * 
     * @param id 商品管理表主键
     * @return 商品管理表
     */
    @Override
    public SUM selectManagementById(Integer id)
    {

        Basic basic = basicMapper.selectBasicById(id);
        if (basic!=null){
            Details details = detailsMapper.selectDetailsById(basic.getBasicSnowflakesId());
            Logistics logistics = logisticsMapper.selectLogisticsById(basic.getBasicSnowflakesId());
            Marketing marketing = marketingMapper.selectMarketingById(basic.getBasicSnowflakesId());
            Specification specification = specificationMapper.selectSpecificationById(basic.getBasicSnowflakesId());
            Other other = otherMapper.selectOtherById(basic.getBasicSnowflakesId());
            SUM sum = new SUM();
            sum.setDetails(details);
            sum.setBasic(basic);
            sum.setLogistics(logistics);
            sum.setMarketing(marketing);
            sum.setSpecification(specification);
            sum.setOther(other);
            return sum;
        }else {
            return null;
        }
    }

    /**
     * 查询商品管理表列表
     * 
     * @param management 商品管理表
     * @return 商品管理表
     */
    @Override
    public List<Management> selectManagementList(Management management)
    {
        List<Management> managements = managementMapper.selectManagementList(management);
        return managements;
    }

    /**
     * 新增商品管理表
     * 
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int insertManagement(SUM sum)
    {
        Basic basic = sum.getBasic();
        String uuid = String.valueOf(UUID.randomUUID());
        basic.setBasicSnowflakesId(uuid);
        int i=managementMapper.insertBasic(basic);
        Logistics logistics = sum.getLogistics();
        logistics.setSnowflakesId(uuid);
        int i1 = managementMapper.insertLogistics(logistics);
        Details details = sum.getDetails();
        details.setSnowflakesId(uuid);
        int i2 = managementMapper.insertDetails(details);
        Marketing marketing = sum.getMarketing();
        marketing.setSnowflakesId(uuid);
        int i3 = managementMapper.insertMarketing(marketing);
        Other other = sum.getOther();
        other.setSnowflakesId(uuid);
        int i4 = managementMapper.insertOther(other);
        Specification specification = sum.getSpecification();
        specification.setSnowflakesId(uuid);
        int i5 = managementMapper.insertSpecification(specification);
        if (i5>0){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 修改商品管理表
     * 
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int updateManagement(SUM sum)
    {
        basicMapper.updateBasic(sum.getBasic());
        logisticsMapper.updateLogistics(sum.getLogistics());
        detailsMapper.updateDetails(sum.getDetails());
        marketingMapper.updateMarketing(sum.getMarketing());
        otherMapper.updateOther(sum.getOther());
        int i5 = specificationMapper.updateSpecification(sum.getSpecification());
        if (i5>0){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 批量删除商品管理表
     * 
     * @param ids 需要删除的商品管理表主键
     * @return 结果
     */
    @Override
    public int deleteManagementByIds(Long[] ids)
    {
        return managementMapper.deleteManagementByIds(ids);
    }

    @Override
    public List<ProductCategory> treeselect() {
        return managementMapper.treeselect();
    }

    @Override
    public List<SpecProductVo> specList() {
        return managementMapper.specList();
    }

    /**
     * 删除商品管理表信息
     * 
     * @param id 商品管理表主键
     * @return 结果
     */
    @Override
    public int deleteManagementById(Long id)
    {
        return managementMapper.deleteManagementById(id);
    }

    @Override
    public String change(Management management) {
       int i=managementMapper.change(management);
       if (i==1){
           return "修改成功";
       }else {
           return "修改失败";
       }
    }

    @Override
    public List<SUM> selectSumList(Management management) {
        return managementMapper.selectSumList(management);
    }
}
